import {observable, action} from "mobx";
import {Reply, Comment, RouteLayout} from "weaver-mobile-page";
import {Bubble, Tools, Toast, Modal} from "weaver-mobile";
import * as API from "../../api";

const getLabel = Tools.getLabel

export default class ReplyEditorStore {
    constructor(params) {
        if (params && params.getComment) {
            this.source = params.source
            this.commentStore = new Comment.Store({
                getComment: params.getComment,
                operate: this.doOperateReply
            });
        }
    }

    @observable commentStore = null;

    @observable _discusstype;
    @observable _sortid = null;
    @observable source = null;
    @observable routePath = null;
    @observable accsec = null;
    @observable contentText = "";
    @observable imageArr = [];
    @observable onReload = () => {
    };
    @observable layout = new RouteLayout.Store({route: `/${this.routePath}/layout`});
    @observable history = null;


    getAttachList = () => {
        if (this.source.prjid) {
            const params = {
                sortid: this.source.prjid,
                pageindex: 1
            }
            return API.getPrjExchange(params).then(action(res => {
                let attachList = [];
                this.accsec = res.accsec
                res.replyTypes.forEach((item) => {
                    if (item === "acc") {
                        attachList.push({
                            key: "acc",
                            type: "upload",
                            path: this.routePath + "/upload",
                            config: {
                                maxUploadSize: res.accsize,
                                multiSelection: true,
                                uploadUrl: `/api/proj/prjutil/fileUpload?accsec=${res.accsec}`
                            },
                            iconType: "file",
                            iconBgColor: "#F3B04C",
                            text: getLabel("156", "附件"),
                        })
                    }
                    if (item === "crm") {
                        attachList.push({
                            key: "crm",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: 7,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "relevant-customer",
                            iconBgColor: "#FB8D00",
                            text: getLabel("136", "客户"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }
                    if (item === "prj") {
                        attachList.push({
                            key: "prj",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: 8,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "relevant-project",
                            iconBgColor: "#009EFB",
                            text: getLabel("101", "项目"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }

                    if (item === "task") {
                        attachList.push({
                            key: "task",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: "prjtsk",
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "related-tasks",
                            iconBgColor: "#7FACBA",
                            text: getLabel("1332", "任务"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }
                    if (item === "wf") {
                        attachList.push({
                            key: "wf",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: 16,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "flow",
                            iconBgColor: "#61A8F5",
                            text: getLabel("30045", "流程"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }
                    if (item === "doc") {
                        attachList.push(
                            {
                                key: "doc",
                                type: "browser",
                                path: this.routePath,
                                config: {
                                    type: 9,
                                    isSingle: false,
                                },
                                iconType: "file",
                                iconBgColor: "#F3B04C",
                                text: getLabel("22243", "文档"),
                                model: 1,
                            },
                        )
                    }
                })
                return {attachList};
            }))
        } else if (this.source.taskid) {
            const params = {
                sortid: this.source.taskid,
                pageindex: 1
            }
            return API.getTaskExchange(params).then(action(res => {
                let attachList = [];
                res.replyTypes.forEach((item) => {
                    if (item === "acc") {
                        attachList.push({
                            key: "acc",
                            type: "upload",
                            path: this.routePath + "/upload",
                            config: {
                                maxUploadSize: res.accsize,
                                multiSelection: true,
                                uploadUrl: `/api/proj/prjutil/fileUpload?accsec=${res.accsec}`
                            },
                            iconType: "enclosure",
                            iconBgColor: "#57D58F",
                            text: getLabel("156", "附件"),
                            model: 1,
                        })
                    }
                    if (item === "crm") {
                        attachList.push({
                            key: "crm",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: 7,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "relevant-customer",
                            iconBgColor: "#FB8D00",
                            text: getLabel("136", "客户"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }
                    if (item === "prj") {
                        attachList.push({
                            key: "prj",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: 8,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "relevant-project",
                            iconBgColor: "#009EFB",
                            text: getLabel("101", "项目"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }

                    if (item === "task") {
                        attachList.push({
                            key: "task",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: "prjtsk",
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "related-tasks",
                            iconBgColor: "#7FACBA",
                            text: getLabel("1332", "任务"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }
                    if (item === "wf") {
                        attachList.push({
                            key: "wf",
                            type: "browser",
                            path: this.routePath,
                            config: {
                                type: 16,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: "flow",
                            iconBgColor: "#61A8F5",
                            text: getLabel("30045", "流程"),
                            model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
                        })
                    }
                    if (item === "doc") {
                        attachList.push(
                            {
                                key: "doc",
                                type: "browser",
                                path: this.routePath,
                                config: {
                                    type: 9,
                                    isSingle: false,
                                },
                                iconType: "file",
                                iconBgColor: "#F3B04C",
                                text: getLabel("22243", "文档"),
                                model: 1,
                            },
                        )
                    }
                })
                return {attachList};
            }))
        }


    }
    getAtConfig = () => (new Promise((resolve) => {
        resolve({
            atConfig: {
                key: "at",
                path: this.routePath,
                config: {
                    type: 16,
                    isSingle: false,
                },
            },
        });
    }))
    @observable reply = new Reply.Store({getAttachList: this.getAttachList, getAtConfig: this.getAtConfig});


    @action
    setRoutePath = (pathname) => {
        this.routePath = pathname;
    }
    @action
    setSortID = (id) => {
        this._sortid = id;
    }
    @action
    setDiscussType = (type) => {
        this._discusstype = type;
    }
    @action
    recordHistory = (history) => {
        this.history = history;
    }
    @action
    clearInfomation = () => {
        this.imageArr = [];
        this.accsec = null;
        this.contentText = "";
    }

    @action
    submitReply = (data, replyRef) => {
        Toast.loading("Loading...", 10000);
        const complexTypes = {...data.attach};
        for (let i in complexTypes) {
            if (complexTypes[i]) {
                let temp = [];
                complexTypes[i].forEach((item) => {
                    temp.push(item.id);
                })
                complexTypes[i] = temp.join(",");
            }
        }
        const params = {
            sortid: this._sortid,
            discusstype: this._discusstype,
            remark: data.text + this.imageArr.join("    "),
            opttype: "add",
            ...complexTypes
        }
        API.doExchangeOperation(params).then(action(res => {
            Toast.hide();
            if (res.status) {
                this.onReload()
                Bubble.success(getLabel("500835", "相关交流发表成功！"), 1);
                this.clearInfomation();
                this.reply.doClear();
                this.commentStore.getDatas();
                if(replyRef){
                    replyRef.maskClick();
                }
            } else {
                Bubble.error(res.msg, 1)
            }
            
        }))
    }
    @action
    doOperateReply = (item) => {
        const {data, type: {type}} = item;
        if (type === "delete") {
            Modal.alert(getLabel("15172", "系统提示"), getLabel("15097", "确定要删除吗？"), [
                {text: getLabel("31129", "取消")},
                {
                    text: getLabel("826", "确定"), onPress: () => {
                        Toast.loading("Loading...", 10000);
                        const params = {
                            id: data.replyId,
                            opttype: "delete"
                        }
                        API.doExchangeOperation(params).then(action(res => {
                            if (res.status) {
                                Bubble.success(getLabel("83472", "删除成功！"))
                                this.onReload()
                                this.commentStore.getDatas();
                            }
                            Toast.hide()
                        }))
                    }
                }])
        } else if (type === "editor") {
            this.history.push(this.layout.path)
        }
    }

}
